package com.xd.common.security.util;

import cn.hutool.json.JSONUtil;
import com.xd.user.api.entity.User;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 *@program: cloud-exam
 *@description: 解析jwtToken
 *@author: 胡博欣
 *@create: 2019-09-11-59
 */
@Data
public class JwtParse {

    /**
     * 解析token
     * @param accessToken token
     * @param publicKey 公钥
     * @return User
     */
    public static User jwtTokenParse(String accessToken, String publicKey){
        Jwt jwt = JwtHelper.decodeAndVerify(accessToken, new RsaVerifier(publicKey));
        Map map = JSONUtil.toBean(jwt.getClaims(), HashMap.class);
        return JSONUtil.toBean(JSONUtil.toJsonStr(map.get("user")),User.class);
    }
}
